using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using AlphaWebsite.Models;

namespace AlphaWebsite.Controllers
{   
    public class UserPersonalInformationsController : Controller
    {
		private readonly ICityRepository cityRepository;
		private readonly ICityDistrictRepository citydistrictRepository;
		private readonly IUserPersonalInformationRepository userpersonalinformationRepository;

		// If you are using Dependency Injection, you can delete the following constructor
        public UserPersonalInformationsController() : this(new CityRepository(), new CityDistrictRepository(), new UserPersonalInformationRepository())
        {
        }

        public UserPersonalInformationsController(ICityRepository cityRepository, ICityDistrictRepository citydistrictRepository, IUserPersonalInformationRepository userpersonalinformationRepository)
        {
			this.cityRepository = cityRepository;
			this.citydistrictRepository = citydistrictRepository;
			this.userpersonalinformationRepository = userpersonalinformationRepository;
        }

        //
        // GET: /UserPersonalInformations/

        public ViewResult Index()
        {
            return View(userpersonalinformationRepository.AllIncluding(userpersonalinformation => userpersonalinformation.City, userpersonalinformation => userpersonalinformation.District));
        }

        //
        // GET: /UserPersonalInformations/Details/5

        public ViewResult Details(System.Guid? id)
        {
            return View(userpersonalinformationRepository.Find(id));
        }

        //
        // GET: /UserPersonalInformations/Create

        public ActionResult Create()
        {
			ViewBag.PossibleCities = cityRepository.All;
			ViewBag.PossibleDistricts = citydistrictRepository.All;
            return View();
        } 

        //
        // POST: /UserPersonalInformations/Create

        [HttpPost]
        public ActionResult Create(UserPersonalInformation userpersonalinformation)
        {
            if (ModelState.IsValid) {
                userpersonalinformationRepository.InsertOrUpdate(userpersonalinformation);
                userpersonalinformationRepository.Save();
                return RedirectToAction("Index");
            } else {
				ViewBag.PossibleCities = cityRepository.All;
				ViewBag.PossibleDistricts = citydistrictRepository.All;
				return View();
			}
        }
        
        //
        // GET: /UserPersonalInformations/Edit/5
 
        public ActionResult Edit(System.Guid? id)
        {
			ViewBag.PossibleCities = cityRepository.All;
			ViewBag.PossibleDistricts = citydistrictRepository.All;
             return View(userpersonalinformationRepository.Find(id));
        }

        //
        // POST: /UserPersonalInformations/Edit/5

        [HttpPost]
        public ActionResult Edit(UserPersonalInformation userpersonalinformation)
        {
            if (ModelState.IsValid) {
                userpersonalinformationRepository.InsertOrUpdate(userpersonalinformation);
                userpersonalinformationRepository.Save();
                return RedirectToAction("Index");
            } else {
				ViewBag.PossibleCities = cityRepository.All;
				ViewBag.PossibleDistricts = citydistrictRepository.All;
				return View();
			}
        }

        //
        // GET: /UserPersonalInformations/Delete/5
 
        public ActionResult Delete(System.Guid? id)
        {
            return View(userpersonalinformationRepository.Find(id));
        }

        //
        // POST: /UserPersonalInformations/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(System.Guid? id)
        {
            userpersonalinformationRepository.Delete(id);
            userpersonalinformationRepository.Save();

            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing) {
                cityRepository.Dispose();
                citydistrictRepository.Dispose();
                userpersonalinformationRepository.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}

